Skip to main content
Get your production-ready VPC infrastructure up and running in just a few steps.

Prerequisites

Before you begin, ensure you have:
  • Terraform installed (version 0.12 or later recommended)
  • AWS credentials configured
  • Basic understanding of VPC concepts

Basic Deployment

1

Create a Terraform configuration file

Create a new file named main.tf with the following content:
main.tf
module "vpc" {
  source = "github.com/Planview/tf_aws_vpc"

  name = "my-vpc"
  cidr = "10.0.0.0/16"
  
  azs             = ["us-west-2a", "us-west-2b", "us-west-2c"]
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
  public_subnets  = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"]
  
  enable_nat_gateway   = true
  enable_dns_hostnames = true
  enable_dns_support   = true
  
  tags = {
    Terraform   = "true"
    Environment = "production"
  }
}
This creates a basic multi-AZ VPC with:
  • 3 public subnets (one per AZ)
  • 3 private subnets (one per AZ)
  • NAT Gateways for private subnet internet access
  • DNS support enabled
2

Add outputs to reference VPC resources

Add this to your main.tf to access the created resources:
main.tf
output "vpc_id" {
  description = "The ID of the VPC"
  value       = module.vpc.vpc_id
}

output "private_subnets" {
  description = "List of IDs of private subnets"
  value       = module.vpc.private_subnets
}

output "public_subnets" {
  description = "List of IDs of public subnets"
  value       = module.vpc.public_subnets
}
3

Initialize Terraform

Run the following command to download the module:
terraform init
4

Review the plan

See what resources will be created:
terraform plan
You should see approximately 15+ resources being created, including:
  • 1 VPC
  • 6 subnets (3 public, 3 private)
  • 1 Internet Gateway
  • 3 NAT Gateways
  • Route tables and associations
5

Apply the configuration

Create the infrastructure:
terraform apply
Type yes when prompted to confirm.
NAT Gateways cost approximately 0.045/hour(0.045/hour (32/month) per gateway. This basic configuration creates 3 NAT Gateways. Consider using single_nat_gateway = true for development environments to reduce costs.
6

Verify the outputs

After the deployment completes, you’ll see the outputs:
Outputs:

vpc_id = "vpc-1234567890abcdef0"
private_subnets = [
  "subnet-1234567890abcdef0",
  "subnet-1234567890abcdef1",
  "subnet-1234567890abcdef2",
]
public_subnets = [
  "subnet-1234567890abcdef3",
  "subnet-1234567890abcdef4",
  "subnet-1234567890abcdef5",
]

Alternative Configurations

For development or staging environments, use a single shared NAT Gateway:
main.tf
module "vpc" {
  source = "github.com/Planview/tf_aws_vpc"

  name = "dev-vpc"
  cidr = "10.0.0.0/16"
  
  azs             = ["us-west-2a", "us-west-2b"]
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
  public_subnets  = ["10.0.101.0/24", "10.0.102.0/24"]
  
  enable_nat_gateway   = true
  single_nat_gateway   = true
  enable_dns_hostnames = true
  
  tags = {
    Terraform   = "true"
    Environment = "development"
  }
}
This reduces NAT Gateway costs by ~66% but sacrifices redundancy.

Using Module Outputs

Once your VPC is deployed, you can use its outputs in other resources:
resource "aws_instance" "app" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t3.micro"
  
  subnet_id              = module.vpc.private_subnets[0]
  vpc_security_group_ids = [aws_security_group.app.id]
  
  tags = {
    Name = "app-server"
  }
}

Available Outputs

The module provides these outputs that you can reference:
OutputDescriptionExample Usage
vpc_idVPC IDSecurity group vpc_id
vpc_cidr_blockVPC CIDR blockNetwork planning
private_subnetsPrivate subnet IDsEC2, ECS, Lambda
public_subnetsPublic subnet IDsLoad balancers, NAT
database_subnetsDatabase subnet IDsRDS instances
elasticache_subnetsElastiCache subnet IDsRedis, Memcached
database_subnet_groupDB subnet group nameRDS configuration
elasticache_subnet_groupElastiCache subnet group nameElastiCache configuration
natgw_idsNAT Gateway IDsMonitoring, cost tracking
nat_eips_public_ipsNAT Gateway public IPsWhitelisting
igw_idInternet Gateway IDRoute table configuration
public_route_table_idsPublic route table IDsCustom routes
private_route_table_idsPrivate route table IDsCustom routes
default_security_group_idDefault security group IDSecurity group rules
vpc_endpoint_s3_idS3 VPC Endpoint IDPolicy configuration
vpc_endpoint_dynamodb_idDynamoDB VPC Endpoint IDPolicy configuration
See the Outputs Reference for complete details.

Cleanup

To destroy the VPC and all associated resources:
terraform destroy
This will permanently delete all resources created by the module. Ensure you have no running resources (EC2 instances, RDS databases, etc.) using the VPC before destroying it.

Next Steps

Configuration Options

Explore all available configuration variables

Architecture Guide

Understand the VPC architecture and design

Examples

See more detailed deployment examples

Reference

Complete input and output reference